package cm.android.mytodoit.view;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import cm.android.mytodoit.R;
import cm.android.mytodoit.controller.Todo_SQLOpenHelper;
import cm.android.mytodoit.model.TodoElement;

public class DetailTodoview extends Activity {

	private EditText title, description, priority, expirationdate;
	private SQLiteDatabase database;
	private Todo_SQLOpenHelper sqlOpenHelper;
	private Button saveButton, finishButton;
	private int todoID;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.detailtodoview);

		this.title = (EditText) findViewById(R.id.et_detailtodo_todotitle);
		this.description = (EditText) findViewById(R.id.et_detailtodo_description);
		this.priority = (EditText) findViewById(R.id.et_detailtodo_priority);
		this.expirationdate = (EditText) findViewById(R.id.et_detailtodo_expirationdate);
		
		this.saveButton = (Button) findViewById(R.id.bu_detailtodo_save);
		this.finishButton = (Button) findViewById(R.id.bu_detailtodo_finishbutton);
				
		this.sqlOpenHelper = new Todo_SQLOpenHelper(this);
	}
	
	public void onButtonClick(View view) {
		
		switch (view.getId()) {
		case R.id.bu_detailtodo_finishbutton:
			
			SQLiteDatabase database = sqlOpenHelper.getWritableDatabase();
			
			database.delete(TodoElement.TABLE_NAME, TodoElement.TC_ID +" = " +this.todoID, null);
			
			database.close();
			
			this.finish();
			
			break;
		case R.id.bu_detailtodo_save:
			
			SQLiteDatabase database2 = sqlOpenHelper.getWritableDatabase();
			
			ContentValues values = new ContentValues();
			values.put(TodoElement.TC_TITLE, this.title.getText().toString());
			values.put(TodoElement.TC_DESCRIPTION, this.description.getText().toString());
			values.put(TodoElement.TC_EXPIRATIONDATE, this.expirationdate.getText().toString());
			values.put(TodoElement.TC_PRIORITY, this.priority.getText().toString());
			
			database2.update(TodoElement.TABLE_NAME, values, TodoElement.TC_ID + " = "+ this.todoID, null);
			
			database2.close();
			
			this.finish();
			
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		this.database = this.sqlOpenHelper.getReadableDatabase();

		Cursor cursor = this.database.query(TodoElement.TABLE_NAME, null, TodoElement.TC_ID
				+ " = "
				+ this.getIntent().getExtras().getInt(TodoElement.TC_ID), null,
				null, null, null);

		while (cursor.moveToNext()) {
			this.title.setText(cursor.getString(cursor.getColumnIndex(TodoElement.TC_TITLE)));
			this.description.setText(cursor.getString(cursor.getColumnIndex(TodoElement.TC_DESCRIPTION)));
			this.priority.setText(cursor.getString(cursor.getColumnIndex(TodoElement.TC_PRIORITY)));
			this.expirationdate.setText(cursor.getString(cursor.getColumnIndex(TodoElement.TC_EXPIRATIONDATE)));
			this.todoID = cursor.getInt(cursor.getColumnIndex(TodoElement.TC_ID));
		}
		
	}

	@Override
	protected void onPause() {
		database.close();
		super.onPause();
	}
}
